package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.ShopSpecificationSku;
import com.addplus.townmall.server.api.model.town.expand.ShopSpecificationSkuExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品sku表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface ShopSpecificationSkuMapper extends BaseAddMapper<ShopSpecificationSku> {

    @Insert("<script>" +
            "insert into shop_specification_sku(" +
            "shop_product_id," +
            "sku_id," +
            "sku_describe," +
            "stock," +
            "specification_code," +
            "sale_price," +
            "cost_price," +
            "line_price," +
            "discount_price," +
            "is_default," +
            "type," +
            "is_deleted," +
            "gmt_create," +
            "gmt_modified" +
            ") values" +
            "<foreach collection='list' item='item'  separator=','>" +
            "(" +
            "#{item.shopProductId}," +
            "#{item.skuId}," +
            "#{item.skuDescribe}," +
            "#{item.stock}," +
            "#{item.specificationCode}," +
            "#{item.salePrice}," +
            "#{item.costPrice}," +
            "#{item.linePrice}," +
            "#{item.discountPrice}," +
            "#{item.isDefault}," +
            "#{item.type}," +
            "#{item.isDeleted}," +
            "now()," +
            "now()" +
            ")" +
            "</foreach>" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void batchInsert(@Param("list")List<ShopSpecificationSku> list);

    @Select("<script>\n" +
            "SELECT\n" +
            " sku.id,\n" +
            " sku.shop_product_id,\n" +
            " sku.sku_describe,\n" +
            " sku.stock,\n" +
            " sku.sale_price,\n" +
            " sku.cost_price,\n" +
            " sku.line_price,\n" +
            " sku.discount_price,\n" +
            " sku.sku_id,\n" +
            " sku.specification_code,\n" +
            " sku.is_default,\n" +
            " a.shop_active_type,\n" +
            " a.shop_obj_id,\n" +
            " a.status \n" +
            "FROM\n" +
            " shop_specification_sku sku\n" +
            " LEFT JOIN shop_participate_active a ON sku.shop_product_id = a.shop_product_id \n" +
            " AND a.shop_sku_id = sku.id \n" +
            " AND a.`status` = 1 \n" +
            " AND a.is_deleted = 0\n" +
            "WHERE\n" +
            " sku.shop_product_id = #{shopProductId} \n" +
            " <if test=\"skuId !=null and skuId !='' \">\n" +
            " AND sku.id = #{skuId}\n" +
            " </if>\n" +
            " AND sku.is_deleted = 0 \n" +
            " AND sku.type = #{type} \n" +
            "ORDER BY\n" +
            " sku.is_default DESC,\n" +
            " sku.sale_price ASC,\n" +
            " a.shop_obj_id DESC" +
            "</script>")
    List<ShopSpecificationSkuExpand> getShopSpecificationSkuAndActive(@Param("shopProductId") Integer shopProductId, @Param("skuId") Integer skuId, @Param("type") Integer type);

}
